/**
 * Created by dell on 2015/12/1.
 */
var mongodb = require('mongodb');
// var DB_CONN_STR = 'mongodb://133.133.133.38:27017/AppCheck';
var DB_CONN_STR = 'mongodb://127.0.0.1:27017/AppCheck';
var MongoClient = require('mongodb').MongoClient;
var logger = require('./logger.js').getLogger('DbConn');

var collections = ['test'];

var dbManager = {};

// Initialize connection once
dbManager.connect = function(callback){
    MongoClient.connect(DB_CONN_STR, function(err, database) {
        if(err){
            logger.error(err);
        }else{
            dbManager.db = database;
            callback();
        }
    });
};
dbManager.queryDisplayFields = function (collectionName, queryStr, displayFields, successCallback, failedCallback) {
    var collection = dbManager.db.collection(collectionName);
    try{
        var promise = collection.find(queryStr, {fields: displayFields}).toArray();
        promise.then(successCallback, failedCallback);
    }catch(err){
        logger.error(err.message);
    }
};
// not promise because it would compromise the escapsulation as the response should return when error occured
dbManager.query = function(collectionName, queryStr, successCallback, failedCallback){
    var collection = dbManager.db.collection(collectionName);
    // the find function would throw exception before the query is executed
    try{
        var promise = collection.find(queryStr).toArray();
        promise.then(successCallback, failedCallback);
    }catch(err){
        logger.error(err.message);
    }
};

dbManager.queryOne = function(collectionName, queryStr, successCallback, failedCallback){
    var collection = dbManager.db.collection(collectionName);
    // the find function would throw exception before the query is executed
    try{
        var promise = collection.findOne(queryStr);
        promise.then(successCallback, failedCallback);
    }catch(err){
        logger.error(err.message);
    }
};

dbManager.insertData = function(collectionName, data, successCallback, failedCallback) {
    var collection = dbManager.db.collection(collectionName);
    var promise = collection.insert(data);
    promise.then(successCallback, failedCallback);
};

dbManager.queryFields = function(collectionName, queryStr, queryFields, successCallback, failedCallback){
    var collection = dbManager.db.collection(collectionName);
    try{
        var promise = collection.find(queryStr, {fields: queryFields}).toArray();
        promise.then(successCallback,failedCallback);
    }catch(err){
        logger.error(err.message);
    }
};

dbManager.deleteByFields = function (collectionName, queryStr, successCallback, failedCallback) {
    var collection = dbManager.db.collection(collectionName);
    var promise = collection.deleteOne(queryStr);
    promise.then(successCallback, failedCallback);
};

dbManager.queryFieldsAndSort = function (collectionName, queryStr, queryFields, sortField, successCallback, failedCallback) {
    var collection = dbManager.db.collection(collectionName);
    try{
        var promise = collection.find(queryStr, {fields: queryFields}).sort([[sortField, 1]]).toArray();
        promise.then(successCallback,failedCallback);
    }catch(err){
        logger.error(err.message);
    }
};

dbManager.queryFieldsByPage = function (collectionName, queryStr, queryFields, sortField, start, num, successCallback, failedCallback) {
    var collection = dbManager.db.collection(collectionName);
    try{
        var promise = collection.find(queryStr, {fields: queryFields}).sort([[sortField, 1]]).skip(start).limit(num).toArray();
        promise.then(successCallback, failedCallback);
    }catch(err){
        logger.error(err.message);
    }
};

module.exports = dbManager;

